﻿using MyMusicPlay._4._0.Operate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace MyMusicPlay._4._0
{
    public class DisPlayTextAnim : BaseClass
    {
        public TextBlock TextBlock;
        public StackPanel StackPanel;
        public MainWindow mainWindow;
        public Storyboard TextAnim = new Storyboard()
        {
            AutoReverse = true
        };

        public DisPlayTextAnim(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;
        }

        public void Initialize()
        {
            StackPanel = mainWindow.DisPlayPlayName_StackPanel;
            TextBlock = mainWindow.CurMusicName;

            TextAnim.Completed += EventHandler;
        }

        public void BeginAnim()
        {
            //设置文字
            TextBlock.Text = PlayApplication.GetModule<Media>().CurPlayMusicString;

            TextAnim.Stop(TextBlock);
            var maxWidth = StackPanel.MaxWidth;
            var curWidth = GetTextDisplayWidth.GetWidth(TextBlock);
            if (curWidth > maxWidth)
            {
                //加载成动画
                Animate.Move_IsStop(TextAnim, TextBlock, new Thickness(0, 30, 0, 0), new Thickness((curWidth - maxWidth) * -1, 30, 0, 0), (curWidth - maxWidth) * 50, anim => { anim.BeginTime = TimeSpan.FromMilliseconds(1000); });
            }
        }

        public void EventHandler(object? sender, EventArgs e)
        {
            var maxWidth = StackPanel.MaxWidth;
            var curWidth = GetTextDisplayWidth.GetWidth(TextBlock);
            Animate.Move_IsStop(
                TextAnim, 
                TextBlock, 
                new Thickness(0, 30, 0, 0), 
                new Thickness((curWidth - maxWidth) * -1, 30, 0, 0), 
                (curWidth - maxWidth) * 50, 
                anim => { anim.BeginTime = TimeSpan.FromMilliseconds(1000); });
        }
    }
}
